/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.hiv.common;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 * @version NaiYao.java, v 0.1 2023/3/24 22:31 下午 NaiYao Exp $$
 */
@Data
public class NaiYaoRequest {

    @JsonProperty("operationName")
    private String operationName = "SequenceAnalyses";
    @JsonProperty("variables")
    private VariablesDTO variables;
    @JsonProperty("query")
    private String query = "query SequenceAnalyses($sequences: [UnalignedSequenceInput]!, $includeGenes: [EnumGene!]!, $algorithms: [ASIAlgorithm!], $customAlgorithms: [CustomASIAlgorithm!]) {\n  __typename\n  currentVersion {\n    text\n    publishDate\n    __typename\n  }\n  currentProgramVersion {\n    text\n    publishDate\n    __typename\n  }\n  sequenceAnalysis(sequences: $sequences) {\n    inputSequence {\n      header\n      __typename\n    }\n    ...ReportBySequences\n    __typename\n  }\n  ...ReportBySequencesRoot\n}\n\nfragment ReportBySequencesRoot on Root {\n  currentVersion {\n    display\n    __typename\n  }\n  mutationPrevalenceSubtypes {\n    name\n    stats(includeGenes: $includeGenes) {\n      gene {\n        name\n        __typename\n      }\n      totalNaive\n      totalTreated\n      __typename\n    }\n    __typename\n  }\n  __typename\n}\n\nfragment ReportBySequences on SequenceAnalysis {\n  inputSequence {\n    header\n    __typename\n  }\n  strain {\n    name\n    display\n    __typename\n  }\n  availableGenes {\n    name\n    __typename\n  }\n  bestMatchingSubtype {\n    display\n    referenceAccession\n    __typename\n  }\n  subtypes: subtypesV2(first: 10) {\n    displayWithoutDistance\n    subtype {\n      displayName\n      __typename\n    }\n    distancePcnt\n    referenceAccession\n    referenceCountry\n    referenceYear\n    __typename\n  }\n  validationResults(includeGenes: $includeGenes) {\n    level\n    message\n    __typename\n  }\n  mutationPrevalences(includeGenes: $includeGenes) {\n    boundMutation {\n      gene {\n        name\n        __typename\n      }\n      text\n      position\n      reference\n      triplet\n      __typename\n    }\n    matched {\n      AA\n      subtypes {\n        subtype {\n          name\n          __typename\n        }\n        percentageNaive\n        percentageTreated\n        __typename\n      }\n      __typename\n    }\n    others {\n      AA\n      subtypes {\n        subtype {\n          name\n          __typename\n        }\n        percentageNaive\n        percentageTreated\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n  algorithmComparison(\n    algorithms: $algorithms\n    customAlgorithms: $customAlgorithms\n  ) {\n    drugClass {\n      name\n      __typename\n    }\n    drugScores {\n      drug {\n        name\n        displayAbbr\n        __typename\n      }\n      algorithm\n      SIR\n      interpretation\n      explanation\n      __typename\n    }\n    __typename\n  }\n  drugResistance(includeGenes: $includeGenes) {\n    algorithm {\n      text\n      family\n      version\n      publishDate\n      __typename\n    }\n    gene {\n      name\n      drugClasses {\n        name\n        fullName\n        __typename\n      }\n      __typename\n    }\n    levels: drugScores {\n      drugClass {\n        name\n        __typename\n      }\n      drug {\n        name\n        displayAbbr\n        fullName\n        __typename\n      }\n      text\n      __typename\n    }\n    mutationsByTypes {\n      drugClass {\n        name\n        __typename\n      }\n      mutationType\n      mutations {\n        text\n        AAs\n        reference\n        position\n        isUnusual\n        isApobecMutation\n        isApobecDRM\n        isDRM\n        DRMDrugClass {\n          name\n          fullName\n          __typename\n        }\n        isUnsequenced\n        totalReads\n        allAAReads {\n          aminoAcid\n          numReads\n          percent\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    commentsByTypes {\n      commentType\n      comments {\n        name\n        text\n        highlightText\n        boundMutation {\n          position\n          text\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    drugScores {\n      drugClass {\n        name\n        __typename\n      }\n      drug {\n        name\n        displayAbbr\n        __typename\n      }\n      score\n      level\n      text\n      SIR\n      partialScores {\n        mutations {\n          text\n          __typename\n        }\n        score\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n  alignedGeneSequences(includeGenes: $includeGenes) {\n    firstAA\n    lastAA\n    gene {\n      name\n      length\n      __typename\n    }\n    mutations {\n      text\n      AAs\n      unusualAAs\n      reference\n      position\n      primaryType\n      isApobecMutation\n      hasStop\n      isUnsequenced\n      isUnusual\n      isAmbiguous\n      isDRM\n      DRMDrugClass {\n        name\n        fullName\n        __typename\n      }\n      totalReads\n      triplet\n      allAAReads {\n        aminoAcid\n        numReads\n        percent\n        __typename\n      }\n      __typename\n    }\n    unsequencedRegions {\n      size\n      regions {\n        posStart\n        posEnd\n        __typename\n      }\n      __typename\n    }\n    prettyPairwise {\n      positionLine\n      refAALine\n      alignedNAsLine\n      mutationLine\n      __typename\n    }\n    sdrms: mutations(filterOptions: [SDRM]) {\n      text\n      __typename\n    }\n    frameShifts {\n      text\n      position\n      isInsertion\n      isDeletion\n      __typename\n    }\n    __typename\n  }\n  __typename\n}";

    public NaiYaoRequest() {
    }

    /**
     * 获取查询参数
     * @param list
     * @return
     */
    public static NaiYaoRequest getRequest(List<String> list) {
        NaiYaoRequest.SequencesDTO sequences = new NaiYaoRequest.SequencesDTO();
        sequences.setHeader(list.get(0));
        list.remove(0);
        String sequence = list.stream().collect(Collectors.joining(""));
        sequences.setSequence(sequence);

        NaiYaoRequest request = new NaiYaoRequest();
        VariablesDTO variables = new VariablesDTO();
        variables.setSequences(Lists.newArrayList(sequences));
        request.setVariables(variables);

        return request;
    }

    @Data
    @NoArgsConstructor
    public static class VariablesDTO {
        @JsonProperty("sequences")
        private List<SequencesDTO> sequences = Collections.emptyList();
        @JsonProperty("algorithms")
        private List<String> algorithms = Collections.emptyList();
        @JsonProperty("customAlgorithms")
        private List<String> customAlgorithms = Collections.emptyList();
        @JsonProperty("includeGenes")
        private List<String> includeGenes = Lists.newArrayList("PR", "RT", "IN");
    }

    @Data
    @NoArgsConstructor
    public static class SequencesDTO {
        @JsonProperty("header")
        private String header;
        @JsonProperty("sequence")
        private String sequence;
    }
}
